package com.force.abril.prevision.download;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.abril.prevision.Prevision;
import br.com.abril.prevision.PrevisionService;

import com.force.abril.prevision.download.transformer.PrevisionTransformer;
import com.force.provider.SalesForceServices;
import com.sforce.soap.partner.QueryResult;
import com.sforce.ws.ConnectionException;

@Service
public class PrevisionServiceActivator implements PrevisionSelector {

	@Autowired
	private SalesForceServices selector;
	
	@Autowired
	private PrevisionService service;
	
	@Autowired
	private PrevisionTransformer tranformer;
	
    public PrevisionServiceActivator() {
    }

   
    public void downloadPrevisions () throws ConnectionException {
    	QueryResult result = selector.query(SELECT);
    	boolean hasMoreRecords = false;
    	while (!hasMoreRecords) {    	
    		service.allPrevision().save(tranformer.toPrevisionsFromSObject( result.getRecords()));
    		if (!result.getDone()) {
    			result = selector.queryMore(result.getQueryLocator());
    			continue;
    		}
    		hasMoreRecords = result.getDone();
    	}  
    	
    }

    
    
    
    
}
